/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package br.com.jr2tecnologia;

import br.com.jr2tecnologia.model.LoginPersistenceLocal;
import br.com.jr2tecnologia.model.entity.Login;
import java.io.IOException;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author ronildo
 */

@WebServlet(value="/loginListener")
public class LoginListener extends HttpServlet{

    private static final Logger logger = LoggerFactory.getLogger();

    @EJB
    private LoginPersistenceLocal loginFacade;

    protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String email = request.getParameter("email");
        String activationKey = request.getParameter("activation_key");
        if(email == null || activationKey == null){
            response.sendRedirect(ConstantsOfSystem.LNK_REGISTER_INVALID);
            return;
        }

        Login login = loginFacade.findLoginByEmail(email);
        if(login != null && login.getActivationKey().equals(activationKey)){
            login.setBlocked(false);
            loginFacade.update(login);
            response.sendRedirect(ConstantsOfSystem.LNK_REGISTER_VALID);
        }else{
            response.sendRedirect(ConstantsOfSystem.LNK_REGISTER_INVALID);
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    public String getServletInfo() {
        return "Servlet LoginListener";
    }
}